package com.gome.axwl.aoc.standalone.config.quartz;

import com.gome.axwl.aoc.web.controller.quartzJob.CarrierSettleJob;
import com.gome.axwl.aoc.web.controller.quartzJob.DriverIntegrationJob;
import com.gome.axwl.aoc.web.controller.quartzJob.RuleInitJob;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.SchedulingException;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import static org.quartz.CronScheduleBuilder.cronSchedule;

/**
 * @Description: quartz调度器
 * @Auther: yinyuxin@gome.com.cn
 * @Date: 2018/5/2 15:47
 */
@Component
public class QuartzScheduler {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    /**
     * 每月一号凌晨一点
     */
    private static final String SCHEDULE_CRON="0 0 1 1 * ?";
   // private static final String SCHEDULE_CRON="0 */1 * * * ?";

    /**
     * 每周一上午11点
     */
    private static final String SCHEDULE_CRON_CARRIER_SETTEL="0 0 11 ? * MON";

    private static final Logger LOGGER=LoggerFactory.getLogger(QuartzScheduler.class);

    /**
     * 在spring容器启动时，执行此方法
     * @author yinyuxin
     */
    @PostConstruct
    public void init() throws JobExecutionException {
        this.scheduleJobs();
    }

    public void scheduleJobs() throws SchedulingException, JobExecutionException {
        LOGGER.info("==================quartz分布式定时任务初始化================");
        Scheduler scheduler=schedulerFactoryBean.getScheduler();
       JobDetail jobDetail=JobBuilder.newJob(DriverIntegrationJob.class).withDescription("司机积分等级计算定时任务").withIdentity("calculateIntegration-ininit","integration-group").build();
        Trigger trigger=TriggerBuilder.newTrigger().forJob(jobDetail).withSchedule(cronSchedule(SCHEDULE_CRON)).build();
        JobDetail jobDetail2=JobBuilder.newJob(RuleInitJob.class).withDescription("司机积分等级规则定时任务").withIdentity("rule-ininit","rule-group").build();
        Trigger trigger2=TriggerBuilder.newTrigger().forJob(jobDetail2).withSchedule(cronSchedule(SCHEDULE_CRON)).build();
        JobDetail jobDetail3=JobBuilder.newJob(CarrierSettleJob.class).withDescription("提醒运营审批服务商入驻申请单定时任务").withIdentity("approval-carrierSettle","approval-group").build();
        Trigger trigger3=TriggerBuilder.newTrigger().forJob(jobDetail3).withSchedule(cronSchedule(SCHEDULE_CRON_CARRIER_SETTEL)).build();

        try {
            scheduler.clear();
           if (!scheduler.checkExists(JobKey.jobKey("calculateIntegration-ininit","integration-group"))){
                scheduler.scheduleJob(jobDetail,trigger);
            }
            if (!scheduler.checkExists(JobKey.jobKey("rule-ininit","rule-group"))){
                scheduler.scheduleJob(jobDetail2,trigger2);
            }
            if (!scheduler.checkExists(JobKey.jobKey("approval-carrierSettle","approval-group"))){
                scheduler.scheduleJob(jobDetail3,trigger3);
            }
            scheduler.start();
            LOGGER.info("==================quartz分布式定时任务启动成功================");
        } catch (Exception e) {
            LOGGER.error("==================quartz分布式定时任务出现异常:{}================",ExceptionUtils.getFullStackTrace(e));
            JobExecutionException e2=new JobExecutionException(e);
            //job产生异常后，立即重启所有的触发器
            e2.setRefireImmediately(true);
        }
    }
}
